//
//  RepeatImageView.m
//  UGoods
//
//  Created by dllo on 16/1/19.
//  Copyright © 2016年 蓝鸥科技.李贺. All rights reserved.
//

#import "RepeatImageView.h"
#import <UIImageView+WebCache.h>
@interface RepeatImageView ()<UIScrollViewDelegate>

@property (nonatomic, assign) NSInteger index;

@end


@implementation RepeatImageView

- (void)dealloc
{
    [_scrollView release];
    [_imageView release];
    [_alabel release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    return self;
}

+ (instancetype)RepeatImageViewWithFrame:(CGRect)frame imageArray:(NSMutableArray *)imageArray {
    RepeatImageView *repeat = [[RepeatImageView alloc] initWithFrame:frame];
    [repeat RepeatImageViewWithFrame:frame imageArray:imageArray];
    return [repeat autorelease];
}

- (void)RepeatImageViewWithFrame:(CGRect)frame imageArray:(NSMutableArray *)imageArray {
    /**< scrollView */
    self.scrollView = [[UIScrollView alloc] initWithFrame:frame];
    self.scrollView.backgroundColor = [UIColor blackColor];
    self.scrollView.contentSize = CGSizeMake(frame.size.width * (imageArray.count + 2), 0);
    self.scrollView.contentOffset = CGPointMake(frame.size.width, 0);
    self.scrollView.pagingEnabled = YES;
    self.scrollView.bounces = NO;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.delegate = self;
    [self addSubview:self.scrollView];
    [_scrollView release];
    
    //
    UIImageView *firstImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:[imageArray lastObject]] placeholderImage:[UIImage imageNamed:@"place"]];
    [self.scrollView addSubview:firstImage];
    [firstImage release];
    
    UIImageView *lastImage = [[UIImageView alloc] initWithFrame:CGRectMake(frame.size.width * (imageArray.count + 1), 0, frame.size.width, frame.size.height)];
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:imageArray[0]] placeholderImage:[UIImage imageNamed:@"place"]];    [self.scrollView addSubview:lastImage];
    [lastImage release];
    
    //
    for (int i = 0; i < imageArray.count; i++) {
        self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(frame.size.width * (i + 1), 0, frame.size.width, frame.size.height)];
        [self.imageView sd_setImageWithURL:[NSURL URLWithString:imageArray[i]] placeholderImage:[UIImage imageNamed:@"place"]];
        self.imageView.tag = 1000 + i;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        tap.numberOfTapsRequired = 1;
        tap.numberOfTouchesRequired = 1; // 单击
        self.imageView.userInteractionEnabled = YES;
        [self.imageView addGestureRecognizer:tap];
        [self.scrollView addSubview:self.imageView];
        [_imageView release];
        [tap release];
    }
    
    self.alabel = [[UILabel alloc] initWithFrame:CGRectMake(frame.size.width - 50, frame.size.height - 50, 40, 30)];
    self.alabel.text = [NSString stringWithFormat:@"%ld/%ld", self.index, imageArray.count];
    [self.scrollView addSubview:self.alabel];
    [_alabel release];
}

#pragma mark - UIImageView 点击事件
- (void)tapAction:(UITapGestureRecognizer *)tap {
    if ([self.delegate respondsToSelector:@selector(didClickImage:)]) {
        [self.delegate didClickImage:tap.view.tag];
    }
}

#pragma mark - 循环滑动
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    self.index = self.scrollView.contentOffset.x / self.scrollView.frame.size.width - 1;
    if (self.scrollView.contentOffset.x <= 0) {
        self.scrollView.contentOffset = CGPointMake(self.scrollView.frame.size.width * (self.scrollView.contentSize.width / self.scrollView.frame.size.width - 2), 0);
        self.index = self.scrollView.contentOffset.x / self.scrollView.frame.size.width - 1;
    } else if (self.scrollView.contentOffset.x >= self.scrollView.frame.size.width * (self.scrollView.contentSize.width / self.scrollView.frame.size.width - 1)) {
        self.scrollView.contentOffset = CGPointMake(self.scrollView.frame.size.width, 0);
        self.index = self.scrollView.contentOffset.x / self.scrollView.frame.size.width - 1;
    }
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
